package com.collins.thread.practices.findPrime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.*;

public class FindPrime {
    public static final Integer NUM_SIZE = 1000;
    public static final Integer NUM_THREADS = 10;
    public static final Integer INTERVAL = NUM_SIZE / NUM_THREADS;
    public static ArrayList<Integer> num = new ArrayList<>();
    public static Random rand = new Random(42);
    public static ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);

    public static void main(String[] args) {
        init();
        // TODO create one costumed thread pool
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,10,
                1000,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(10),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < NUM_THREADS; i++) {
            FutureTask<ArrayList<Integer>> ft = new FutureTask<>(new FindPrimeThread(INTERVAL, i, num));
            executor.execute(ft);
            ArrayList<Integer> list = null;
            try {
                list = ft.get();
                System.out.println(list.toString());
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        executor.shutdown();
    }

    public static void init() {
        for (int i = 0; i < NUM_SIZE; i++) {
            num.add(rand.nextInt(999));
        }
        Collections.shuffle(num);

    }

}
